package donnees;

import java.util.ArrayList;

import enumerations.Cond;
import exceptions.VariableException;

public class Contrainte {
	
	private int indice;
	private ArrayList<Variable> vars;
	private ArrayList<Double> coeffs;
	private Cond cond;
	private double cste;

	public Contrainte(int indice, ArrayList<Variable> vars, ArrayList<Double> coeffs, Cond cond, double cste) throws VariableException {
		if(coeffs.size() != vars.size()) {
			throw new VariableException(0, "Contrainte.Contrainte()");
		}
		// on parcourt la liste des variables pour voir s'il n'y a pas 2 fois la meme
		for(int i = 0; i < vars.size(); ++i) {
			for(int j = 0; j < vars.size(); ++j) {
				if(i != j && vars.get(i).getNom().equals(vars.get(j).getNom())) {
					throw new VariableException(1,"Contrainte.Contrainte()");
				}
			}
		}
		this.indice = indice;
		this.vars = vars;
		this.coeffs = coeffs;
		this.cond = cond;
		this.cste = cste;
	}
	
	public void setCoeffs(Variable var, Double coeff) throws VariableException {
		int position = coeffs.indexOf(var);
		if(position == -1) {
			throw new VariableException(2,"Contrainte.setCoeff()");
		}
		else {
			this.coeffs.set(position,coeff);
		}
	}

	public Cond getCond() {
		return cond;
	}

	public void setCond(Cond cond) {
		this.cond = cond;
	}

	public double getCste() {
		return cste;
	}

	public void setCste(double cste) {
		this.cste = cste;
	}

	public int getIndice() {
		return indice;
	}

	public void setIndice(int indice) {
		this.indice = indice;
	}

	public void addVar(Variable var, Double coeff) throws VariableException {
		// on parcourt la liste des variables pour voir s'il n'y a pas 2 fois la meme
		for(int i = 0; i < vars.size(); ++i) {
			if(vars.get(i).getNom().equals(var.getNom())) {
				throw new VariableException(1,"Contrainte.addVar()");
			}
		}
		vars.add(var);
		coeffs.add(coeff);
	}

	public boolean includeVar(Variable var) {
		int i = 0;
		while(i < vars.size() && !vars.get(i).getNom().equals(var.getNom())) {
			++i;
		}
		if(i == vars.size()) {
			return false;
		}
		else {
			return true;
		}
	}

	public void removeVar(Variable var) throws VariableException {
		int pos = vars.indexOf(var);
		if(pos == -1) {
			throw new VariableException(2,"Contrainte.removeVar()");
		}
		else {
			vars.remove(pos);
			coeffs.remove(pos);
		}
	}
	
	public void setContrainte(int indice, ArrayList<Variable> vars, ArrayList<Double> coeffs, Cond cond, double cste) throws VariableException {
		if(coeffs.size() != vars.size()) {
			throw new VariableException(0, "Contrainte.Contrainte()");
		}
		// on parcourt la liste des variables pour voir s'il n'y a pas 2 fois la meme
		for(int i = 0; i < vars.size(); ++i) {
			for(int j = 0; j < vars.size(); ++j) {
				if(i != j && vars.get(i).getNom().equals(vars.get(j).getNom())) {
					throw new VariableException(1,"Contrainte.Contrainte()");
				}
			}
		}
		this.indice = indice;
		this.vars = vars;
		this.coeffs = coeffs;
		this.cond = cond;
		this.cste = cste;
	}
	
	public ArrayList<Variable> getVariables() {
		return vars;
	}
	
	public ArrayList<Double> getCoeffs() {
		return coeffs;
	}
}
